﻿using System.Collections;
using System.Collections.Generic;
using UI;
using UnityEngine;

public class MeteorEffect : MonoBehaviour
{
    public Vector3 centerPosition = new Vector3(100, 100, 0);

    private Vector3 startPosition;
    private Vector3 endPosition;

    private float effectDuration = 0.5f;
    private float starTime = 0;
    private bool IsLocal = true;
    private bool IsFly = true;
    private void Update()
    {
        if (IsFly)
        {
            if (starTime < effectDuration)
            {
                if (IsLocal)
                {
                    transform.localPosition = GetCurvePos(startPosition, endPosition, centerPosition, starTime / effectDuration);
                }
                else
                {
                    transform.position = GetCurvePos(startPosition, endPosition, centerPosition, starTime / effectDuration);
                }
                starTime += Time.unscaledDeltaTime;
            }
            else
            {
                IsFly = false;
                if (IsLocal)
                {
                    transform.localPosition = GetCurvePos(startPosition, endPosition, centerPosition, 1);
                }
                else
                {
                    transform.position = GetCurvePos(startPosition, endPosition, centerPosition, 1);
                }
            }
        }
    }

    public static void GenMeteorEffect(Transform transRoot, Vector3 start, Vector3 end, bool isLocal = true)
    {
        UIEffectManager.Instance.PlayEffectImmediate(UIEffectResId.EffectId_AutoChess_Meteor, transRoot, (obj) =>
        {
            obj.transform.SetParent(transRoot);
            int childCount = obj.transform.childCount;
            for (int i = 0; i < childCount; i++)
            {
                var child = obj.transform.GetChild(i);
                var trail = child.GetComponent<TrailRenderer>();
                if (trail != null)
                {
                    trail.Clear();
                }
            }
            var meteor = obj.AddComponent<MeteorEffect>();

            if (meteor != null)
            {
                meteor.SetMeteorEffect(start, end, isLocal);
            }
        });
    }

    /// <summary>
    /// 生成贝塞尔曲线路径
    /// </summary>
    /// <param name="transRoot">特效挂点</param>
    /// <param name="start">起始坐标</param>
    /// <param name="end">结束坐标</param>
    /// <param name="center">拐点坐标, 始终使用挂点下的局部坐标</param>
    /// <param name="isLocal">其实和结束坐标是否为本地空间，否则是世界坐标</param>
    public static void GenMeteorEffect(Transform transRoot, Vector3 start, Vector3 end, Vector3 center, bool isLocal = true)
    {
        UIEffectManager.Instance.PlayEffectImmediate(UIEffectResId.EffectId_AutoChess_Meteor, transRoot, (obj) =>
        {
            obj.transform.SetParent(transRoot);
            int childCount = obj.transform.childCount;
            for (int i = 0; i < childCount; i++)
            {
                var child = obj.transform.GetChild(i);
                var trail = child.GetComponent<TrailRenderer>();
                if (trail != null)
                {
                    trail.Clear();
                }
            }
            var meteor = obj.AddComponent<MeteorEffect>();

            if (meteor != null)
            {
                meteor.SetMeteorEffect(start, end, center, isLocal);
            }
        });
    }

    /// <summary>
    /// 生成贝塞尔曲线路径
    /// </summary>
    /// <param name="starPos">起始坐标</param>
    /// <param name="endPos">结束坐标</param>
    /// <param name="centerPos">拐点坐标, 始终使用挂点下的局部坐标</param>
    /// <param name="isLocal">是否为本地坐标</param>
    private void SetMeteorEffect(Vector3 starPos, Vector3 endPos, Vector3 centerPos, bool isLocal)
    {
        startPosition = starPos;
        centerPosition = centerPos;
        endPosition = endPos;
        if (IsLocal)
        {
            transform.localPosition = starPos;
        }
        else
        {
            transform.position = starPos;
            centerPosition = transform.localToWorldMatrix * centerPosition;
        }
    }

    private void SetMeteorEffect(Vector3 starPos, Vector3 endPos, bool isLocal)
    {
        startPosition = starPos;
        endPosition = endPos;
        if (IsLocal)
        {
            transform.localPosition = starPos;
        }
        else
        {
            transform.position = starPos;
        }
    }
    private Vector3 GetCurvePos(Vector3 startPos, Vector3 endPos, Vector3 peakPos, float ratio)
    {
        Vector3 vec = Vector3.zero;
        vec = ((endPos - peakPos) * ratio + peakPos - (peakPos - startPos) * ratio - startPos) * ratio + startPos + (peakPos - startPos) * ratio;
        return vec;
    }
}
